package com.situ.mall.service.impl;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.mall.common.ServerResponse;
import com.situ.mall.dao.ProductMapper;
import com.situ.mall.entity.Product;
import com.situ.mall.service.IProductService;

@Service
public class ProductServiceImpl implements IProductService{
	@Autowired
	private ProductMapper productMapper;

	@Override
	public ServerResponse list() {
		List<Product> list = productMapper.selectAll();
		return ServerResponse.createSuccess("成功", list);
	}

	@Override
	public ServerResponse insert(Product product) {
		int count = productMapper.insert(product);
		if (count == 1) {
			return ServerResponse.createSuccess("添加商品成功");
		}
		
		return ServerResponse.createError("添加商品失败");
	}

	@Override
	public Product selectById(Integer productId) {
		return productMapper.selectByPrimaryKey(productId);
	}

	@Override
	public void exportExcel(HttpServletResponse response) {
		//查询所有商品列表
		List<Product> list = productMapper.selectAll();
		for (Product product : list) {
			System.out.println(product);
		}
		
		XSSFWorkbook workbook = new XSSFWorkbook();
		XSSFSheet sheet = workbook.createSheet("用户列表");
		//设置默认的列宽
		sheet.setDefaultColumnWidth(15);
		//创建合并单元格对象
		CellRangeAddress cellRangeAddress = new CellRangeAddress(2, 6, 1, 8);
		sheet.addMergedRegion(cellRangeAddress);
		XSSFRow row = sheet.createRow(2);
		XSSFCell cell = row.createCell(1);
		
		XSSFCellStyle style = createStyle(workbook, 30);
		//将创建好的样式加到单元格上
		cell.setCellStyle(style);
		cell.setCellValue("产品列表");
		
		//创建表格表头
		String[] array = {"ID", "商品名称", "商品副标题", "价格", "库存"};
		XSSFRow rowHead = sheet.createRow(7);
		for (int i = 0; i < array.length; i++) {
			XSSFCell cellHead = rowHead.createCell(i + 1);
			XSSFCellStyle headStyle = createStyle(workbook, 15);
			cellHead.setCellStyle(headStyle);
			cellHead.setCellValue(array[i]);
		}
		//将集合List<Product> list中数据写到excel中
		for (int j = 0; j < list.size(); j++) {
			Product product = list.get(j);
			XSSFRow productRow = sheet.createRow(8 + j);
			XSSFCell cellId = productRow.createCell(1);
			cellId.setCellValue(product.getId());
			XSSFCell cellName = productRow.createCell(2);
			cellName.setCellValue(product.getName());
			XSSFCell cellSubtitle = productRow.createCell(3);
			cellSubtitle.setCellValue(product.getSubtitle());
			XSSFCell cellPrice = productRow.createCell(4);
			cellPrice.setCellValue(product.getPrice() + "");
			XSSFCell cellStock = productRow.createCell(4);
			cellStock.setCellValue(product.getStock());
		}
		
		//以附件的形式返回给浏览器
		ServletOutputStream outputStream = null;
		response.setContentType("application/x-excel");
		try {
			response.setHeader("Content-Disposition", 
					"attachment;filename=" + new String("产品列表.xlsx".getBytes(), "ISO-8859-1"));
			outputStream = response.getOutputStream();
			workbook.write(outputStream);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (workbook != null) {
				try {
					workbook.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private XSSFCellStyle createStyle(XSSFWorkbook workbook, int fontSize) {
		//创建单元格样式
		XSSFCellStyle style = workbook.createCellStyle();
		style.setAlignment(XSSFCellStyle.ALIGN_CENTER);//水平居中
		style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);//垂直居中
		//创建字体
		XSSFFont font = workbook.createFont();
		font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);//字体加粗
		font.setFontHeightInPoints((short)fontSize);//设置字体大小
		//加载字体到样式
		style.setFont(font);
		return style;
	}
}
